package com.os4j.utils;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public abstract class FastjsonUtils {
	
	public JSONObject str2JSONObject(String jsonString){
		return JSON.parseObject(jsonString);
	}
	
	public String object2JSONString(Object o){
		return JSON.toJSONString(o);
	}
	
	public JSONObject object2JSONObject(Object o){
		return str2JSONObject(object2JSONString(o));
	}
	
	public JSONArray list2JSONArray(List<Object> list){
		return JSON.parseArray(object2JSONString(list));
	}
	
	public JSONObject java2JSONOject(Object o){
		return str2JSONObject(object2JSONString(o));
	}
	
	public Object object2Java(Object json,Class clazz){
		return jsonObject2Java((JSONObject)json, clazz);
	}
	
	public Object jsonObject2Java(JSONObject json,Class clazz){
		return JSON.toJavaObject(json, clazz);
	}
	
	public List<Object> JSONArray2JavaList(JSONArray array,Class clazz){
		return array.toJavaList(clazz);
	}
	public Map<String,Object> compartr(Map<String,Object> map,JSONObject jo1,JSONObject jo2){
		Set<String> sk1 = jo1.keySet();
        for (String key : sk1) {
        	Object o1 = jo1.get(key);
        	Object o2 = jo2.get(key);
        	compartr(map,key,o1,o2);
		}
		return map;
	}
	
	public Map<String,Object> compartr(Map<String,Object> map,String key,Object o1, Object o2) {  
    	if (o1 instanceof JSONObject && o2 instanceof JSONObject) {  
        	return compartr(map,(JSONObject) o1, (JSONObject) o2);  
        }  else if (o1  instanceof JSONArray && o2 instanceof JSONArray) {  
        	return compartr(map,key,(JSONArray) o1, (JSONArray) o2);  
        }  else if (o1 == null && o2 == null) {  
        	return null;  
        }    else {
        	return compartr(map,key,o1,o2,true);
        }
    }
	
	public Map<String,Object> compartr(Map<String,Object> map,String key,Object o1,Object o2,boolean base){
		if(o1==null || !o1.equals(o2))
			map.put(key, o1+"|"+o2);
		return map;
	}
	
	public abstract Map<String,Object> compartr(Map<String,Object> map,String key,JSONArray ja1,JSONArray ja2);
	
	public Class getClassByName(String className){
		try {
			return Class.forName(className);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
}
